<template>
    <div class="wrapper">
        <div class="video-box">
            <video class="video-background" preload="auto" loop playsinline autoplay src="../assets/bg1.mp4"
                tabindex="-1" muted="muted"></video>
        </div>

        <div class="login-container" id="login-container" style="visibility:hidden;">
            <h2>用户登陆</h2>
            <form>
                <label for="username1">账号</label>
                <input type="text" id="username1" name="username1">
                <label for="password1">密码</label>
                <input type="password" id="password1" name="password1">
                <div class="reg_log">
                    <button @click="handleLogin" type="button" style="background-color: rgb(255, 255, 255);">登陆</button>
                    <button @click="handleReg" type="button" style="background-color: rgb(0, 110, 255,0.8);">注册</button>
                </div>
            </form>
        </div>

        <div class="login-container" id="reg-container" style="visibility:hidden;">
            <h2>用户注册</h2>
            <form>
                <label for="username2">账号</label>
                <input type="text" id="userId" name="userId">
                <label for="username2">用户名(昵称)</label>
                <input type="text" id="username2" name="username2">
                <label for="password2">密码</label>
                <input type="password" id="password2" name="password2">
                <div class="reg_log">
                    <button @click="Reg" type="button" style="background-color: rgb(0, 110, 255,0.8);">注册</button>
                </div>
            </form>
        </div>


        <div class="touch_to_start" id="touch_to_start">
            <span>TOUCH TO START</span>
        </div>

        <div class="head_img" id="head_img">
            <img src="../assets/heading.png">
        </div>

        <audio id="myAudio" loop>
            <source src="" type="audio/mp3">
        </audio>

    </div>
</template>

<script>
export default {
    name: 'Login',
    data() {
        return {
            id:'',
            username: '',
            password: '',
            reg_username: '',
            reg_password: '',
            user: {
                id:'',
                userName: '',
                password: '',
            },
            musicList: [require('../assets/1.mp3'), require('../assets/2.mp3'), require('../assets/3.mp3'), require('../assets/4.mp3'), require('../assets/5.mp3'),]
        }
    },
    created() {
        this.user = this.$getSessionStorage('user');
    },
    mounted() {
        const audio = document.getElementById("myAudio");
        const randomIndex = Math.floor(Math.random() * this.musicList.length);
        console.log(randomIndex);
        const randomMusic = this.musicList[randomIndex];
        audio.src = randomMusic;
        audio.volume=0.1;
        audio.play().catch(error => {
            console.log("自动播放失败，等待用户交互后播放:", error);
        });
        window.document.onmousedown = function () {
            if (document.getElementById("reg-container").style.visibility != "visible") {
                const loginContainer = document.getElementById("login-container");
                loginContainer.style.visibility = "visible";
                loginContainer.classList.add("animate");
                document.getElementById("touch_to_start").style.visibility = "hidden";
                audio.play();
            }
        }

    },
    methods: {
        // 登录处理
        handleLogin() {
            var username1 = document.getElementById("username1").value;
            var password1 = document.getElementById("password1").value;
            console.log("登录")
            this.$axios.post('UserController/getUserByIdByPass ', this.$qs.stringify({ 
                userId: username1,
                password: password1
            })).then(response => {
                let user = response.data;
                if (user.id==null)
                {
                    alert("用户名或密码不正确！");
                    return;
                }
                else {
                    this.$setSessionStorage('user', user);
                    this.$router.push('/index');    //使用Vue路由跳转
                }
            }).catch(error => {
                console.error(error);
            });
        },
        // 注册切换
        handleReg() {
            const loginContainer = document.getElementById("login-container");
            const regContainer = document.getElementById("reg-container");
            if (regContainer.style.visibility == "hidden") {
                regContainer.style.visibility = "visible";
                regContainer.classList.add("animate"); // 添加动画类
                loginContainer.style.visibility = "hidden";
                loginContainer.classList.remove("animate"); // 移除动画类
            } else {
                regContainer.style.visibility = "hidden";
                regContainer.classList.remove("animate");
                loginContainer.style.visibility = "visible";
                loginContainer.classList.add("animate");
            }
        },
        Reg() {
            var userId = document.getElementById("userId").value;
            var username2 = document.getElementById("username2").value;
            var password2 = document.getElementById("password2").value;
            this.user = {
                userId:userId,
                userName: username2,  // 确保键名与后端实体类属性一致
                password: password2
            };

            console.log(this.user);
            this.$axios.post('UserController/saveUser', this.$qs.stringify(this.user))
                .then(response => {
                    if (response.data > 0) {
                        alert('注册成功！');
                    } else {
                        alert('注册失败！');
                    }
                })
                .catch(error => {
                    console.error('注册请求失败:', error);
                    alert('网络错误，请稍后重试');
                });

            const regContainer = document.getElementById("reg-container");
            const loginContainer = document.getElementById("login-container");

            if (regContainer.style.visibility == "hidden") {
                regContainer.style.visibility = "visible";
                regContainer.classList.add("animate"); // 添加动画类
                loginContainer.style.visibility = "hidden";
                loginContainer.classList.remove("animate"); // 移除动画类
            } else {
                regContainer.style.visibility = "hidden";
                regContainer.classList.remove("animate");
                loginContainer.style.visibility = "visible";
                loginContainer.classList.add("animate");
            }
        },
    }
}
</script>

<style scoped>
.wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: #f5f5f5;
    font-family: Arial, sans-serif;
    overflow: hidden;
    margin: 0;
    padding: 0;
}

.video-box {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    z-index: -999;
}

.video-box .video-background {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 100vw;
    height: 100vh;
    transform: translate(-50%, -50%);
    object-fit: cover;
    border-radius: 0;
}

.login-container {
    position: absolute;
    width: 320px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -60%);
    padding: 32px 24px;
    background: rgb(255, 233, 237, 0.9);
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    /* 移除默认动画绑定 */
    /* animation: slideUp 1.8s ease; */
    opacity: 0;
    /* 初始隐藏 */
}

/* 新增动画触发类 */
.login-container.animate {
    animation: slideUp 1.8s ease forwards;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translate(-50%, 180%);
    }

    to {
        opacity: 1;
        transform: translate(-50%, -60%);
    }
}

.login-container h2 {
    text-align: center;
    margin-bottom: 24px;
    color: #333;
}

.login-container label {
    display: block;
    margin-bottom: 8px;
    color: #555;
}

.login-container input[type="text"],
.login-container input[type="password"] {
    width: 100%;
    padding: 8px 10px;
    margin-bottom: 16px;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

.login-container .reg_log {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    gap: 20px;
}

.login-container .reg_log button {
    width: 13vh;
    padding: 10px;
    transform: skewX(-10deg);
    color: #30312e;
    border: none;
    border-radius: 8px;
    font-size: 20px;
    cursor: pointer;
    margin-top: 2vh;
    font-weight: bold;
}

.login-container button:hover {
    background: #1565c0;
}

.touch_to_start {
    position: absolute;
    top: 92%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 3vh;
    width: 30vw;
    height: 4vh;
    text-align: center;
    line-height: 4vh;
    background: linear-gradient(to right,
            rgba(255, 255, 255, 0),
            /* 左侧透明 */
            rgba(255, 255, 255, 0.7),
            /* 中间半透明 */
            rgba(255, 255, 255, 0.7),
            /* 中间半透明 */
            rgba(255, 255, 255, 0.7),
            /* 中间半透明 */
            rgba(255, 255, 255, 0)
            /* 右侧透明 */
        );
    color: #419df8;
    font-family: Arial, Helvetica, sans-serif;
    font-weight: 540;

    user-select: none;
}

.head_img {
    position: absolute;
    top: 0%;
    left: 0%;
    z-index: 999;
}

.head_img img {
    width: 20vw;
}
</style>